package com.geo.geotool.service.geofile.fileCharset;


import com.geo.geotool.util.file.CpdetectorEncoding;
import com.geo.geotool.util.file.FileUtils;

import java.io.File;
import java.nio.charset.Charset;

/**
 * • @Description: 通用文件编码格式
 * • @author  : yell
 * • @date : 2021/12/10  12:32
 */
public class ComFileCharsetCapture implements IFileCharsetCapture{
    @Override
    public String getCharsetName(String filePath) throws Exception {
        CpdetectorEncoding cpdetectorEncoding = new CpdetectorEncoding();
        /**获取文件编码*/
        Charset charsetName = cpdetectorEncoding.getFastEncoding(FileUtils.readFileToByteArray(new File(filePath)));
        if (charsetName.name().equals("x-EUC-TW")) {
            charsetName = Charset.forName("GBK");
        }
        return charsetName.name();
    }
}
